package com.salary.snippet

import scala.xml.{NodeSeq,Text}
import net.liftweb.http.{FileParamHolder,S,SHtml,StatefulSnippet, RequestVar}
import net.liftweb.common.{Box,Empty,Full}
import net.liftweb.util.Helpers.{bind,chooseTemplate}
import net.liftweb.util.Helpers._
import net.liftweb.mapper._
import java.io.File
import java.io.FileOutputStream;
import java.io.FileInputStream

import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFCell
import org.apache.poi.hssf.usermodel.HSSFDataFormatter
import org.apache.poi.ss.usermodel.Cell
import snippet._
import model.SocialSecurity

class FileUpload extends TableComm{
	override val sheetName = "杭州"
	def buildEntry(xhtml : NodeSeq, snx:Int) : NodeSeq = {
		val db:List[SocialSecurity] = SocialSecurity.findAll
		var sn:Int = snx
		if (sn >= db.length) {
			sn = db.length - numOfPage
		}
		if (sn < 0 ) sn = 0
		val db1 = db.takeRight(db.length - sn).take(numOfPage)
		db1.flatMap({item => 
				bind("item", chooseTemplate("emp", "table", xhtml),
				"id" -> Text(item.idx),
				"name" -> Text(item.name),
				"base" -> Text(item.base.toString),
				"company" -> Text(item.SAFbyCompany.toString),
				"self" -> Text(item.SAFbySelf.toString) ,
                "actions" -> { SHtml.link("/fileUpload", () => item.delete_!, Text("Delete")) })
		 })
	}
	
	def addSocialS(row: HSSFRow): SocialSecurity = {
		val item = SocialSecurity.create
		item.idx( readCell(row.getCell(1)).toDouble.toInt.toString )
		item.base(readCell(row.getCell(6)).toDouble)
		item.SAFbyCompany(readCell(row.getCell(7)).toDouble)
		item.SAFbySelf(readCell(row.getCell(8)).toDouble)
		item
	}
	override def addItem(row: HSSFRow) = {
		val item = addSocialS(row)
		item.save
	}
	def show(xhtml : NodeSeq) : NodeSeq = {
		println("current value is " + currentSn.is.toString)
		val sn = currentSn.is
		bind("info", xhtml,
			"table" -> buildEntry(xhtml, sn),
			"prev" -> { SHtml.link("/fileUpload", () => currentSn(sn - numOfPage), Text("Prev"))},
			"next" -> { SHtml.link("/fileUpload", () => currentSn(sn + numOfPage), Text("Next"))},
			)
	}
	def upload(xhtml : NodeSeq) : NodeSeq = {
		uploadBase(xhtml)
	}
}